package datos.clases;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import tipos.clases.Donacion;
import tipos.clases.Donante;
import tipos.clases.Usuario;

import datos.interfaces.IDonacionDAO;

public class JDBCDonacionDAO implements IDonacionDAO{

	
	public void inserta(String alias, Integer cantidad) {
		Connection c = ConnectionManager.getInstance().checkOut();		
		PreparedStatement stmt = null;
		
		 String sql = "INSERT INTO Donacion(Donante,Cantidad) VALUES (?,?)";	
	        
	        try {
	        	
	            stmt = c.prepareStatement(sql);
	            stmt.setString(1,alias);
	            stmt.setInt(2,cantidad);	            
	            stmt.executeUpdate();

	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
		
	}

	
	public void actualiza(String alias, Integer cantidad) {
		Connection c = ConnectionManager.getInstance().checkOut();		
		PreparedStatement stmt = null;
            
        String sql = "UPDATE Donacion SET cantidad=cantidad + ? WHERE(Donante = ?)";	
        
        try {
        	
            stmt = c.prepareStatement(sql);
            stmt.setInt(1, cantidad);
            stmt.setString(2, alias);                    
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        
	}	
	
	public List select(){
		Connection c = ConnectionManager.getInstance().checkOut();
		List res = new LinkedList();
		PreparedStatement stmt = null;
        ResultSet result = null;        
        String sql = "SELECT * FROM Donacion";
        
        try{
        	if(c==null){
        		System.out.println("No hay conexion");
        	}
        	stmt = c.prepareStatement(sql);      
        	stmt.executeQuery(sql);
            result = stmt.executeQuery();    
            while (result.next()) {
                Donante temp = new Donante();               
                temp.setDonante(result.getString("Donante"));
                temp.setCantidad(result.getInt("Cantidad"));                   
                res.add(temp);
            }
            
        }catch(SQLException e){
        	System.out.println("Error");
        }finally{
        	  try {
                  if (result != null) {
                      result.close();
                  }
                  if (stmt != null) {
                      stmt.close();
                  }
              } catch (SQLException e) {
              }
        }
		return res;
	}
	

}
